//var config = require('config.js');
//var common = require('common.js');

const app = getApp();

const request = (url, options) => {
  return new Promise((resolve, reject) => {
    wx.request({
      // {app.globalData.host}为接口请求中的公共部分写在app.js中
      //url: `${config.host}${url}`,
      url: 'https://sst.jmit.cn/api_sst' + `${url}`,
      method: options.method,
      //data: options.method === 'GET' ? options.data : JSON.stringify(options.data),
      //header: {
      //  'Content-Type': 'application/json; charset=UTF-8',
      //},
      data: options.data,
      header: {
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
      },
      success(request) {
        if (request.data.code === 200) {
          resolve(request.data);
        } else {
          reject(request.data);
        }
      },
      fail(error) {
        reject(error.data);
      },
    });
  });
};

const get = (url, options = {}) => {
  return request(url, {
    method: 'GET',
    data: options
  });
};
const post = (url, options) => {
  return request(url, {
    method: 'POST',
    data: options
  });
};
const put = (url, options) => {
  return request(url, {
    method: 'PUT',
    data: options
  });
};
// 不能声明DELETE（关键字）
const remove = (url, options) => {
  return request(url, {
    method: 'DELETE',
    data: {
      options
    }
  });
};

const log = (options) => {
  const app = getApp();
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]
  const pagePath = currentPage.route

  options.user_id = app.globalData.userId // 可以添加用户ID等信息
  options.user_identity = app.globalData.userIdentity // 可以添加用户ID等信息
  options.path = pagePath

  console.log('path', pagePath)
  console.log('api.log', options)

  return request('/saveOprtLog.aspx', {
    method: 'POST',
    data: options
  });
};

module.exports = {
  get,
  post,
  put,
  remove,
  log,
};